package com.tensquare.base.controller;

import com.tensquare.base.pojo.Label;
import com.tensquare.base.service.LabelService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("label")
@CrossOrigin
public class LabelController {
    @Autowired
    private LabelService labelService;

    /**
     * 1.查询所有的标签
     * @return  返回所有标签信息和状态
     */
    @GetMapping
    public Result findAll(){
        try {
            List<Label> labelList = labelService.findAll();
            return new Result(StatusCode.OK,true,"查询所有标签成功！",labelList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"查询所有标签失败！");
        }
    }

    /**
     * 2.查询所有推荐的标签
     * @return 返回所有推荐的标签和状态
     */
    @GetMapping("toplist")
    public Result findByRecommend(){
        try {
            List<Label> labels = labelService.findByRecommend();
            return new Result(StatusCode.OK,true,"查询所有推荐标签成功！",labels);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"查询所有推荐标签失败！");
        }
    }

    /**
     * 3.查询所有有效的标签信息
     * @return 返回所有有效标签和状态信息
     */
    @GetMapping("list")
    public Result findByState(){
        try {
            List<Label> labels = labelService.findByState();
            return new Result(StatusCode.OK,true,"查询所有有效标签成功！",labels);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"查询所有有效标签失败！");
        }
    }

    /**
     * 4.根据标签id查询对应的标签信息
     * @param labelId 标签id
     * @return 对应的标签信息和状态信息
     */
    @GetMapping("{labelId}")
    public Result findOne(@PathVariable String labelId){
        try {
            Label label = labelService.findOne(labelId);
            return new Result(StatusCode.OK,true,"根据id查询标签成功！",label);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"根据id查询标签失败！");
        }
    }

    /**
     * 5.添加标签信息
     * @param label 要添加的标签
     * @return 返回是否添加成功的状态信息
     */
    @PostMapping
    public Result add(@RequestBody Label label){
        try {
            labelService.add(label);
            return new Result(StatusCode.OK,true,"添加标签成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"添加标签失败！");
        }
    }

    /**
     * 6.根据id和标签信息修改标签
     * @param labelId   标签id
     * @param label     标签信息
     * @return 是否修改成功的状态信息
     */
    @PutMapping("{labelId}")
    public Result update(@PathVariable String labelId,@RequestBody Label label){
        try {
            label.setId(labelId);
            labelService.update(label);
            return new Result(StatusCode.OK,true,"修改标签成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"修改标签失败！");
        }
    }

    /**
     * 7.根据标签id删除标签信息
     * @param labelId   标签id
     * @return 是否删除成功的状态信息
     */
    @DeleteMapping("{labelId}")
    public Result delete(@PathVariable String labelId){
        try {
            labelService.delete(labelId);
            return new Result(StatusCode.OK,true,"删除标签成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"删除标签失败！");
        }
    }

    /**
     * 8.条件查询
     * @param label 查询条件
     * @return  查询的标签信息
     */
    @PostMapping("search")
    public Result search(@RequestBody Label label){
        try {
            List<Label> labels = labelService.search(label);
            return new Result(StatusCode.OK,true,"条件查询成功！",labels);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"条件查询失败！");
        }
    }

    @PostMapping("search/{page}/{size}")
    public Result search(@PathVariable int page,@PathVariable int size,@RequestBody Label label){
        try {
            PageResult<Label> pageResult = labelService.search(page,size,label);
            return new Result(StatusCode.OK,true,"分页查询成功！",pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(StatusCode.ERROR,false,"分页查询失败！");
        }
    }
}
